package com.vteApp.bl;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.vteApp.dal.RiskDBAdapter;

public class MockRickDB {

	public static MockRickDB instance;
	ArrayList<Risk> risks;
	RiskDBAdapter adabter;
	Context context;

	private MockRickDB(Context context) {
		this.context = context;
		risks = new ArrayList<Risk>();
		getAllRisks();
	}

	public static MockRickDB getInstance(Context context) {
		if (instance == null)
			instance = new MockRickDB(context);
		return instance;
	}

	public ArrayList<Risk> getRisks() {
		return risks;
	}

	void getAllRisks() {
		if (adabter == null) {
			adabter = new RiskDBAdapter(context);
			adabter.open();
		}

		Cursor cursor = adabter.fetchAllRisks();
		if (cursor.moveToFirst()) {
			do {
				risks.add(new Risk(cursor.getInt(0), cursor.getInt(1), cursor
						.getString(2), false, getBooleanValue(cursor.getInt(4))));

			} while (cursor.moveToNext());

		}
	}

	private boolean getBooleanValue(int i) {
		if (i == 1)
			return true;
		return false;

	}

	public void saveAllChanges() {
		for (Risk risk : risks) {
			adabter.updateRisk(risk.getId(), risk.getValue());
		}
	}
}
